
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Logging;


namespace Localization
{
	public class LocalizationBizRule : Syrinx.BaseBizRules
    {
		/// <summary>
		/// Returns a list of every country defined in the countries table.
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public IBizObjList getAllKnownCountries(IApplication app, IRuleContext context)
		{
			IBizObjList countries = null;
			return countries;
		}

		/// <summary>
		/// Returns a list of countries in the CompanyCountries table for a given company.  These
		/// are the countries that the company wants exposed to the user for what country then can choose
		/// for addresses.
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public IBizObjList getCompanyPreferredCountries(IApplication app, IRuleContext context)
		{
			IBizObjList countries = null;
			return countries;
		}

		/// <summary>
		/// This rule will first delete all of the active company's preffered countries and then add all the countries
		/// in the given list.  This is ok, because there are not enough countries in the world to worry about
		/// performance.  
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <param name="companyId"></param>
		/// <param name="countries"></param>
		public void setCompanyPreferredCountries(IApplication app, IRuleContext context, IBizObjList countries)
		{
		}

		/// <summary>
		/// Gets a list of countries for a given company that have cultures that refer to them.
		/// </summary>
		/// <remarks>
		/// Check for "MapUndefinedCountriesToCulture" macro that will have a culture browser code
		/// that is the default culture to use for preferredCountries that dont have a real culture.
		/// If that macro is not defined, the list returned will not contain countries that dont have
		/// a real culture mapped to it in the database.  This allows for a large list of preferredCountries
		/// that can be picked for a culture selection that dont have a fully defined culture defined specifically for
		/// it.  This will be good for sites that want the user to pick what country they are from and the site
		/// will map to a standard culture for that country.
		/// </remarks>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public IBizObjList getCountriesWithActiveCultures(IApplication app, IRuleContext context)
		{
			IBizObjList countries = null;
			return countries;
		}

		/// <summary>
		/// Gets a list of all of the known cultures in the system, regardless of having actual translations
		/// available for them or the company.  There is only one list of known cultures
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public IBizObjList getAllKnownCultures(IApplication app, IRuleContext context)
		{
			IBizObjList countries = null;
			return countries;
		}

		/// <summary>
		/// Gets a list of cultures that have a language with at least one translation for it.
		/// For the active company and are marked as "isActive == true".
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public IBizObjList getActiveCultures(IApplication app, IRuleContext context)
		{
			IBizObjList countries = null;
			return countries;
		}

		/// <summary>
		/// Returns an ActiveCulture type bizobj, with translated text in it.
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <param name="cultureBrowserCode"></param>
		/// <returns></returns>
		public IBizObj getCultureDetailsWithTranslationText(IApplication app, IRuleContext context, string cultureBrowserCode)
		{
			IBizObj activeCulture = null;
			return activeCulture;
		}

		/// <summary>
		/// Creates or updates the given culture (based on culture.isNew field)
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <param name="culture"></param>
		public void createUpdateCulture(IApplication app, IRuleContext context, IBizObj culture)
		{
		}

		public void deleteCulture(IApplication app, IRuleContext context, string cultureBrowserCode)
		{
		}

		public IBizObjList getAllTranslations(IApplication app, IRuleContext context)
		{
			try
			{
				FlexiMap m = new FlexiMap();
				m["companyId"] = ActiveCompanyId;
				IBizObjList translations = app.appData.get("DA.Localization.GetCompanyTranslations", m) as IBizObjList;
				return translations;
			}
			catch (Exception e)
			{
				throw new LocalizationException("Unable to get company Translations", e);
			}
		}

		public IBizObjList getAllTranslationsIncludingBuiltIn(IApplication app, IRuleContext context)
		{
			try
			{
				IBizObjList translations = app.ruleMgr.execute(context, "getAllTranslations").CallResults as IBizObjList;
				if(translations != null)
					translations.Insert(0, app.appData.get("BuiltInTranslation"));

				return translations;
			}
			catch (Exception e)
			{
				throw new LocalizationException("Unable to get company Translations", e);
			}
		}

		public IBizObjList getTranslationsForLanguage(IApplication app, IRuleContext context, string language6392Code)
		{
			IBizObjList translations = null;
			return translations;
		}

		public IBizObjList getTranslationWithTexts(IApplication app, IRuleContext context, Swaf.BGuid.Guid translationId)
		{
			FlexiMap m = new FlexiMap();
			m["id"] = translationId;
			IBizObjList translation = app.appData.get("DA.Localization.GetTranslationTexts", m) as IBizObjList;

			return translation;
		}

		public IBizObjList getTextsForTranslation(IApplication app, IRuleContext context, Swaf.BGuid.Guid translationId)
		{
			context.put("baseId", app.appData.get("BuiltInTranslation.id"));
			context.put("transId", translationId);
			IBizObjList tranText = app.appData.get("DA.Localization.GetTextsForTranslation", context) as IBizObjList;
			return tranText;
		}


		public IBizObjList getUiViewTranslationTexts(IApplication app, IRuleContext context, Swaf.BGuid.Guid translationId)
		{
			FlexiMap m = new FlexiMap();
			m["id"] = translationId;
			IBizObjList translation = app.appData.get("DA.Localization.GetUiControlElementsWithTranslation", m) as IBizObjList;

			return translation;
		}

		public IBizObjList getUiViewControlFields(IApplication app, IRuleContext context, string uiViewName)
		{
			context.put("viewName", uiViewName);
			IBizObjList flds = app.appData.get("DA.Localization.GetUiViewControlFields", context) as IBizObjList;
			return flds;
		}

		public void updateUiViewTranslationText(IApplication app, IRuleContext context, IBizObj translationText)
		{
            try
            {
				translationText.put("trans.textId", translationText.get("textId"));
                int num = (int)app.appData.get("DA.Localization.DeleteUiViewTranslationText", translationText.get("trans"));
				int num2 = (int)app.appData.get("DA.Localization.InsertTranslationText", translationText.get("trans"));
            }
            catch (Exception e)
            {

            }
		}


		/// <summary>
		/// The translationInfo bizobj can contain the texts that make up the translation and those will be inserted/updated
		/// as well.
		/// This will not blindly delete all existing translation texts and re-add them from the translationInfo.  Each
		/// text will be checked for isNew and updated/inserted as needed.  Note that a blank value for the text will cause
		/// the text to be deleted from the database.
		/// </summary>
		/// <param name="app"></param>
		/// <param name="context"></param>
		/// <param name="translationInfo"></param>
		public void createUpdateTranslation(IApplication app, IRuleContext context, IBizObj translationInfo)
		{
			if (translationInfo.isNew)
			{
				app.appData.put("DA.Localization.InsertTranslation", translationInfo);
				context.put("companyId", ActiveCompanyId);
				context.put("translationId", translationInfo.get("id"));
				context.put("visibleToChildCompanies", translationInfo.get("visibleToChildCompanies"));
				app.appData.put("DA.Localization.AssociateTranslationToCompany", context);
			}
			else
			{
				app.appData.put("DA.Localization.UpdateCompanyTranslation", translationInfo);
			}
		}

		public void deleteTranslation(IApplication app, IRuleContext context, Swaf.BGuid.Guid translationId)
		{
			context.put("companyId", ActiveCompanyId);
			context.put("translationId", translationId);
			app.appData.put("DA.Localization.DeleteCompanyTranslation", context);
		}

		public IBizObjList getAllKnownLanguages(IApplication app, IRuleContext context)
		{
			IBizObjList languages = app.appData.get("DA.Localization.GetAllKnownLanguages", new FlexiMap()) as IBizObjList;
			return languages;
		}

		public IBizObjList getLanguagesWithTranslations(IApplication app, IRuleContext context)
		{
			FlexiMap m = new FlexiMap();
			m["companyId"] = ActiveCompanyId;
			IBizObjList languages = app.appData.get("DA.Localization.GetLanguagesWithTranslations", m) as IBizObjList;
			return languages;
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        /// <param name="context"></param>
        /// <param name="contentName"></param>
        /// <returns></returns>
        public IBizObjList getRelatedFieldNameOfUiControls(IApplication app, IRuleContext context, IBizObj obj)
        {
            try
            {
                FlexiMap m = new FlexiMap();
                m["Data"] = obj;
                m["translationId"] = obj.get("translationId");
                IBizObjList languages = app.appData.get("DA.Localization.GetRelatedFieldNameOfUiControls", m) as IBizObjList;
                return languages;
            }
            catch (Exception e)
            {
                throw new LocalizationException("Error executing getRelatedFieldNameOfUiControls", e);
            }
        }

        public void updateRelatedFieldNameOfUiControls(IApplication app, IRuleContext context, IBizObj source)
        {
            IBizObjList lst = (IBizObjList)app.ruleMgr.execute("getRelatedFieldNameOfUiControls", source).CallResults;

            foreach (IBizObj obj in lst)
            {
                obj.put("translatedValue", source.get("translatedValue"));
                obj.put("translationId", source.get("translationId"));
                app.ruleMgr.execute("updateUiViewTranslationText", obj);
            }
        }

		public void postExtendsLocalizeUpdateArticle(IApplication app, IRuleContext context, object returnValue, object[] inputParams)
		{
			IBizObj article = (IBizObj)inputParams[2];
			app.ruleMgr.execute("translateArticle", null, "", new object[] { article });
		}

		
		///// <summary>
		///// 
		///// </summary>
		///// <param name="app"></param>

		///// <param name="context"></param>
		///// <param name="companyId"></param>
		///// <param name="all"></param>
		///// <returns></returns>
		//public IBizObjList getCompanyLanguages(IApplication app, IRuleContext context,Swaf.BGuid.Guid companyId,bool all)
		//{
		//    try
		//    {
		//        FlexiMap m = new FlexiMap();
		//        if (companyId == null)
		//            companyId = ActiveCompanyId;
		//        m.put("companyId", companyId);
		//        m.put("isActive", all);
		//        IBizObjList list = (IBizObjList) app.appData.get("Data.Localization.GetCompanyLanguages", m);
		//        return list;
		//    }
		//    catch (Exception e)
		//    {
		//        throw new LocalizationException("Error executing getCompanyLanguages bizRule.", e);
		//    }
		//}

		///// <summary>
		///// 
		///// </summary>
		///// <param name="app"></param>
		///// <param name="context"></param>
		///// <param name="companyLanguage"></param>
		//public void addCompanyLanguage(IApplication app, IRuleContext context, IBizObj companyLanguage)
		//{
		//    try
		//    {
		//        if (companyLanguage != null)
		//        {
		//            FlexiMap m = new FlexiMap();
		//            m.put("id", new Swaf.BGuid.Guid());
		//            m.put("companyId", companyLanguage.get("companyId"));
		//            m.put("isActive", companyLanguage.get("isActive"));
		//            foreach (IBizObj lang in (IBizObjList)companyLanguage.get("languageDefinitions"))
		//            {
		//                m.put("langId", lang.get("langId"));
		//                m.put("displayName", lang.get("displayName"));
		//                m.put("nativeName", lang.get("nativeName"));
		//                app.appData.put("Data.Localization.AddCompanyLanguage", m);
		//            }
                    
		//        }
		//        else
		//            throw new LocalizationException("CompanyLanguage is null.");
		//    }
		//    catch (LocalizationException le) { throw le; }
		//    catch (Exception e)
		//    {
		//        throw new LocalizationException("Error executing addCompanyLanguage bizRule.", e);
		//    }
		//}

		///// <summary>
		///// 
		///// </summary>
		///// <param name="app"></param>
		///// <param name="context"></param>
		///// <param name="translation"></param>
		//public void addLanguageTranslation(IApplication app, IRuleContext context, IBizObj translation)
		//{
		//    try
		//    {
		//        if (translation != null)
		//        {
		//            translation.put("companyId", ActiveCompanyId);
		//            app.appData.put("Data.Localization.AddlanguageTranslation", translation);
		//        }
		//        else
		//            throw new LocalizationException("Translation is null.");
		//    }
		//    catch (LocalizationException le) { throw le; }
		//    catch (Exception e)
		//    {
		//        throw new LocalizationException("Error executing addTranslation bizRule.", e);
		//    }
		//}

		//public IBizObjList getLanguageTransalations(IApplication app, IRuleContext context, Swaf.BGuid.Guid companyId)
		//{
		//    try
		//    {
		//        if (companyId == null)
		//            companyId=ActiveCompanyId;
		//        FlexiMap m = new FlexiMap();
		//        m.put("companyId", companyId);
		//        IBizObjList list = null;
		//        list = (IBizObjList) app.appData.get("Data.Localization.GetLanguageTranslations", m);
		//        return list;
		//    }
		//    catch (Exception e)
		//    {
		//        throw new LocalizationException("Error executing getLanguageTranslations bizRule.", e);
		//    }
		//}

        
        
    }
}
